package com.ysyc.driver_ee.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.ysyc.driver_ee.R;
import com.ysyc.driver_ee.base.BaseFragment;
import com.ysyc.driver_ee.model.StatistiaVoListModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * <p>描述：(这里用一句话描述这个类的作用)</p>
 * 作者： Chan<br>
 * 日期： 2018/11/10 10:40<br>
 * 版本： v1.0<br>
 */
public class BarChartFragment extends BaseFragment {

    @BindView(R.id.bar_chart)
    BarChart barChart;
    Unbinder unbinder;
    private YAxis leftAxis;             //左侧Y轴
    private YAxis rightAxis;            //右侧Y轴
    private XAxis xAxis;                //X轴
    private Legend legend;              //图例

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bar_chart;
    }

    @Override
    protected void onFragmentCreate(View view) {
        super.onFragmentCreate(view);
        int type = args.getInt(StatisticsPersonalFragment.TYPE_KEY);

        List<StatistiaVoListModel> item = args.getParcelableArrayList("statistiaVoListModels");

        if (0 == type) {
            List<Integer> xValues = new ArrayList<>();
            List<String> yValue1 = new ArrayList<>();
            for (StatistiaVoListModel statistiaVoListModel : item) {
                xValues.add(statistiaVoListModel.water);
                yValue1.add(statistiaVoListModel.time);
            }
            initBarChart(barChart);
            showBarChart(xValues, yValue1, getActivity().getResources().getColor(R.color.histogram_color));
        } else if (1 == type) {
            List<Integer> xValues = new ArrayList<>();
            List<String> yValue1 = new ArrayList<>();
            for (StatistiaVoListModel statistiaVoListModel : item) {
                xValues.add(statistiaVoListModel.num);
                yValue1.add(statistiaVoListModel.time);
            }
            initBarChart(barChart);
            showBarChart(xValues, yValue1, getResources().getColor(R.color.histogram_color));
        }
    }

    private void showBarChart(List<Integer> xValues, List<String> yValue1, int color) {
        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < xValues.size(); i++) {
            /**
             * 此处还可传入Drawable对象 BarEntry(float x, float y, Drawable icon)
             * 即可设置柱状图顶部的 icon展示
             */
            BarEntry barEntry = new BarEntry(i, (float) xValues.get(i));
            entries.add(barEntry);
        }

        // 每一个BarDataSet代表一类柱状图
        BarDataSet barDataSet = new BarDataSet(entries, "");
        initBarDataSet(barDataSet, color);
        //X轴自定义值
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return yValue1.get((int) value % yValue1.size());
            }
        });

        //右侧Y轴自定义值
        leftAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return (int) (value) + "";
            }
        });

        BarData data = new BarData(barDataSet);
        //设置柱状图宽度
        data.setBarWidth(0.5f);
        barChart.setData(data);

    }

    /**
     * 初始化BarChart图表
     */
    private void initBarChart(BarChart barChart) {
        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(false);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);

        barChart.setDoubleTapToZoomEnabled(false);
        //禁止拖拽
        barChart.setDragEnabled(false);
        //X轴或Y轴禁止缩放
        barChart.setScaleXEnabled(false);
        barChart.setScaleYEnabled(false);
        barChart.setScaleEnabled(false);
        //不显示边框
        barChart.setDrawBorders(false);
        //不显示右下角描述内容
        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);

        //设置动画效果
        barChart.animateY(1000, Easing.Linear);
        barChart.animateX(1000, Easing.Linear);

        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setGranularity(1f);
        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //不绘制X Y轴线条
        xAxis.setDrawAxisLine(false);
        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);
        //不显示左侧Y轴
        rightAxis.setEnabled(false);
        //不显示X轴网格线
        xAxis.setDrawGridLines(false);
        //右侧Y轴网格线设置为虚线
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);
    }


    /**
     * 柱状图始化设置 一个BarDataSet 代表一列柱状图
     *
     * @param barDataSet 柱状图
     * @param color      柱状图颜色
     */
    private void initBarDataSet(BarDataSet barDataSet, int color) {
        barDataSet.setColor(color);
        barDataSet.setFormLineWidth(0f);
        barDataSet.setFormSize(0.f);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(true);
        barDataSet.setValueTextSize(10f);
        barDataSet.setValueTextColor(R.color.bar_chart_color);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
